package com.restfinder.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.restfinder.common.component.JsonResponse;
import com.restfinder.common.component.JsonResponseFactory;
import com.restfinder.common.component.ValidationJsonResponse;
import com.restfinder.common.component.utils.JsonSpringUtils;
import com.restfinder.common.component.utils.MessageUtils;
import com.restfinder.common.component.utils.SecurityUtils;
import com.restfinder.restaurant.model.Restaurant;
import com.restfinder.restaurant.service.RestaurantService;
import com.restfinder.user.component.RestaurantValidator;
import com.restfinder.user.component.UserValidator;
import com.restfinder.user.forms.RestaurantForm;
import com.restfinder.user.forms.UserForm;
import com.restfinder.user.model.User;
import com.restfinder.user.service.UserService;

@Controller(value="userrestaurantController")
@SessionAttributes("restaurantForm")
@Slf4j
public class RestaurantController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private RestaurantService restaurantService;
	
	@Autowired
	private RestaurantValidator restaurantValidator;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.setValidator(restaurantValidator);
	}
	
	@RequestMapping(value="/user/{username}/restaurant", method=RequestMethod.GET)
	public ModelAndView restaurantGet(@PathVariable String username){
		String loggedUserUsername = SecurityUtils.getAuthenticatedUsername();
		ModelMap modelMap = new ModelMap();
		if(loggedUserUsername.equalsIgnoreCase(username)){
			User user = userService.getLoggedUser();
			RestaurantForm rf = new RestaurantForm();
			if(user.getRestaurant()==null){
				Restaurant restaurant = new Restaurant();
				restaurant.setOwner(user);
				rf.setRestaurant(restaurant);
			}
			else{
				rf.setRestaurant(user.getRestaurant());
			}
			modelMap.addAttribute("restaurantForm", rf);
			return new ModelAndView("user/restaurant", modelMap);
		}
		else{
			return new ModelAndView("redirect:/");
		}
	}
	
	@RequestMapping(value="/saveRestaurant", method=RequestMethod.POST)
	public @ResponseBody JsonResponse saveRestaurantPOST(@ModelAttribute("restaurantForm") @Valid RestaurantForm restaurantForm, 
			BindingResult result, HttpServletRequest request){
		JsonResponse jsonResponse = JsonResponseFactory.createJsonResponse(result);
		if(!JsonSpringUtils.isSuccessResponse(jsonResponse)){
			jsonResponse.setResult(MessageUtils.createMessage("restaurant.form.error.title", "restaurant.form.error.content"));
			return jsonResponse;
		}
		else{
			try {
				restaurantService.saveOrUpdate(restaurantForm.getRestaurant());
				jsonResponse.setResult(MessageUtils.createMessage("restaurant.form.success.title", "restaurant.form.success.title"));
				return jsonResponse;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("Error while saving restaurant",e);
				jsonResponse = new ValidationJsonResponse();
				jsonResponse.setStatus(JsonSpringUtils.JSON_STATUS_FAILURE);
				jsonResponse.setResult(MessageUtils.createMessage("restaurant.form.error.title", e));
				return jsonResponse;
			}
			
		}		
	}
	
	
}
